package com.wxgo.utils;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class SimpleXMLUtils {

	/**
	 * 读取这种简单的xml <?xml version=\"1.0\" encoding=\"utf-8\"?> <string
	 * xmlns=\"http://tempuri.org/\">-7</string>
	 * 
	 */
	public static String readSimpleXMLStr(String xml) throws ParserConfigurationException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		String result = null;
		try {
			// xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<string
			// xmlns=\"http://tempuri.org/\">-7</string>";
			StringReader sr = new StringReader(xml);
			InputSource is = new InputSource(sr);
			Document doc = builder.parse(is);
			Element ele = doc.getDocumentElement();
			result = ele.getTextContent();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	};
}
